package com.example.day01

import android.os.Bundle
import android.provider.ContactsContract.RawContacts.Data
import android.widget.ArrayAdapter
import android.widget.SimpleAdapter
import android.widget.Spinner
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

//帮我生成10个城市的字符串数组
val citys = listOf("北京", "上海", "广州", "深圳", "杭州", "南京", "西安", "武汉", "成都", "重庆")

class citysActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_citys)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        // 获取Spinner组件
//        val spinner = findViewById<Spinner>(R.id.text)
//
//        // 创建ArrayAdapter适配器
//        val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, citys)
//
//        // 设置下拉列表样式
//        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
//
//        // 将适配器设置给Spinner
//        spinner.adapter = adapter
//
        data class CityData(val icon: Int, val text: String, val intor: String)
//        帮我定义一个含构造函数的类
//
        val citylist = listOf<CityData>(
            CityData(R.drawable.bj, "北京", "北京"),
            CityData(R.drawable.sh, "上海", "上海"),
            CityData(R.drawable.gz, "广州", "广州"),
            CityData(R.drawable.sz, "深圳", "深圳"),
            CityData(R.drawable.hz, "杭州", "杭州"),
            CityData(R.drawable.nj, "南京", "南京"),
        )

        var spCity = findViewById<Spinner>(R.id.text)
        // 为spinner设置适配器，用于显示城市列表
        spCity?.adapter = SimpleAdapter(
            // 当前activity上下文
            this,
            // 将citylist中的CityData对象转换为Map格式的数据列表
            citylist.map {
                // 每个CityData对象映射为包含icon、text、intor键值对的Map
                mapOf("icon" to it.icon, "text" to it.text, "intor" to it.intor)
            },
            // 指定列表项的布局文件
            R.layout.item_icon,
            // 定义Map中的键名数组，用于数据映射
            arrayOf("icon", "text", "intor"),
            // 定义布局文件中对应控件的id数组
            intArrayOf(R.id.icon, R.id.text, R.id.text1)
        )

        spCity?.setSelection(0)
    }
}